package com.jhy.new2025.stack;

import java.util.ArrayDeque;

/**
 * 给你一个字符串数组 tokens ，表示一个根据 逆波兰表示法 表示的算术表达式。
 * 请你计算该表达式。返回一个表示表达式值的整数。
 * '+'、'-'、'*' 和 '/' 。
 * ["2","1","+","3","*"]
 * ((2 + 1) * 3) = 9
 */

public class EvalRPN {

    public static int evalRPN(String[] tokens) {
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        for (int i = 0; i < tokens.length; i++) {
            String temp = tokens[i];
            if (!(temp.equals("+") || temp.equals("-") || temp.equals("*") || temp.equals("/"))) {
                deque.push(Integer.valueOf(temp));
            } else {
                Integer first = deque.pop();
                Integer second = deque.pop();
                Integer sum = 0;
                if (temp.equals("+")) {
                    sum = first + second;
                } else if (temp.equals("-")) {
                    sum = second - first;
                } else if (temp.equals("*")) {
                    sum = first * second;
                } else {
                    sum = second / first;
                }
                deque.push(sum);
            }
        }
        return deque.peek();
    }

    public static void main(String[] args) {
        String[] test = new String[]{"4","13","5","/","+"};
        int i = evalRPN(test);
        System.out.println(i);

    }
}
